package com.csg.ses.service.ses.impl;

import com.csg.ses.common.utils.CommonUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csg.ses.vo.common.Result;
import com.csg.ses.vo.common.ResultStatus;
import lombok.RequiredArgsConstructor;
import com.csg.ses.common.utils.MybatisPlusUtil;
import com.csg.ses.common.utils.SnowFlakeUtil;
import org.springframework.transaction.annotation.Transactional;
import com.csg.ses.vo.common.IdListVo;

import com.csg.ses.dao.ses.SysInterfaceLogDao;
import com.csg.ses.entity.ses.SysInterfaceLogEntity;
import com.csg.ses.service.ses.SysInterfaceLogService;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 接口日志
 * @author wangjun
 * @date 2020-11-18 15:53:40
 */
@Service
@RequiredArgsConstructor
public class SysInterfaceLogServiceImpl extends ServiceImpl<SysInterfaceLogDao, SysInterfaceLogEntity> implements SysInterfaceLogService {
    private final SysInterfaceLogDao sysInterfaceLogDao;

    /**
     * 插入日志
     * @param interfaceName
     * @param interfaceAddress
     * @param sourceSystem
     * @param inParams
     * @return
     */
    @Override
    public Long insertLog(String interfaceName, String interfaceAddress, String sourceSystem, String[] inParams) {
        try {
            SysInterfaceLogEntity log = new SysInterfaceLogEntity();
            Long logId = SnowFlakeUtil.nextLongId();
            log.setId(logId);
            log.setInterfaceName(interfaceName);
            log.setInterfaceAddress(interfaceAddress);
            log.setInterfaceCallTimeStart(new Timestamp(System.currentTimeMillis()));
            log.setSourceSystem(sourceSystem);

            if (inParams != null && inParams.length > 0) {
                for (int i = 0; i < inParams.length; i ++) {
                    if (i == 0) log.setParamIn01(inParams[0]);
                    if (i == 1) log.setParamIn02(inParams[1]);
                    if (i == 2) log.setParamIn03(inParams[2]);
                    if (i == 3) log.setParamIn04(inParams[3]);
                    if (i == 4) log.setParamIn05(inParams[4]);
                    if (i == 5) log.setParamIn06(inParams[5]);
                    if (i == 6) log.setParamIn07(inParams[6]);
                    if (i == 7) log.setParamIn08(inParams[7]);
                    if (i == 8) log.setParamIn09(inParams[8]);
                    if (i == 9) log.setParamIn10(inParams[9]);
                }
            }
            sysInterfaceLogDao.insert(log);
            return logId;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("插入" + interfaceAddress + "接口调用日志出错！");
        }
    }

    /**
     * 更新日志
     * @param logId
     * @param isSuccess
     * @param outParams
     */
    @Override
    public void updateLog(Long logId, Boolean isSuccess, String[] outParams) {
        SysInterfaceLogEntity log = sysInterfaceLogDao.selectById(logId);
        if (log != null) {
            log.setInterfaceCallTimeEnd(new Timestamp(System.currentTimeMillis()));
            log.setIsSuccess(isSuccess ? "Y" : "N");
            if (outParams != null && outParams.length > 0) {
                for (int i = 0; i < outParams.length; i ++) {
                    if (i == 0) log.setParamOut01(outParams[0]);
                    if (i == 1) log.setParamOut02(outParams[1]);
                    if (i == 2) log.setParamOut03(outParams[2]);
                    if (i == 3) log.setParamOut04(outParams[3]);
                    if (i == 4) log.setParamOut05(outParams[4]);
                }
            }
            sysInterfaceLogDao.updateById(log);
        } else {
            throw new RuntimeException("未找到id为" + logId + "的日志记录！");
        }
    }
    /**
     *  日志监控
     * @param isSuccess
     * @param interfaceCallTimeStart
     * @param interfaceCallTimeEnd
     * @return
     */
    @Override
    public Result<List<SysInterfaceLogEntity>> selByIsSuccessAndTime(SysInterfaceLogEntity log, Long current, Long size) {
        try{
            Page<SysInterfaceLogEntity> page = MybatisPlusUtil.initPage(current, size, true);
            QueryWrapper<SysInterfaceLogEntity> qw = new QueryWrapper<>();
            if(!CommonUtil.isEmpty(log.getIsSuccess())){
                qw.eq("is_success", log.getIsSuccess());
            }
            if(!CommonUtil.isEmpty(log.getStartTime())){
                qw.ge("interface_call_time_start",log.getStartTime());
            }
            if(!CommonUtil.isEmpty(log.getEndTime())){
                qw.le("interface_call_time_end",log.getEndTime());
            }
            return new Result(sysInterfaceLogDao.retrieve(page,qw), ResultStatus.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(ResultStatus.FAILED);
        }
    }
}